#include "MenuManager.h"
#include "ui/CocosGUI.h"
#include <cocostudio/cocostudio.h>

#include "../../Framework/SceneManager.h"
#include "../../GameConfig.h"
#include "../Level/LevelManager.h"

using namespace cocostudio;
using namespace ui;

bool MenuManager::m_isProcressing = false;

cocos2d::Scene* MenuManager::createScene()
{
	auto scene = Scene::create();
	auto layer = MenuManager::create();
	scene->addChild(layer);
	return scene;
}

MenuManager::~MenuManager()
{
	//CC_SAFE_FREE(m_node);
	//CC_SAFE_FREE(m_btnSetting);
	//CC_SAFE_FREE(m_pnlSetting);
}

bool MenuManager::init()
{
	if (!Layer::init())
	{
		return false;
	}

	setKeypadEnabled(true);
    m_isProcressing = false;
	m_screenSize = Director::getInstance()->getVisibleSize();

	initalize();

	return true;
}

void MenuManager::initalize()
{
	m_node = CSLoader::getInstance()->createNode("MenuScene.csb");

	float scale = m_screenSize.height / m_node->getContentSize().height;
	m_node->setScale(scale);
	m_node->setAnchorPoint(Vec2(0.5f, 0.5f));
	m_node->setPosition(Vec2(m_screenSize.width / 2, m_screenSize.height / 2));

	initBackground();
	initComponent();
	initButton();

	this->addChild(m_node);
}

void MenuManager::initBackground()
{
	auto background = CSLoader::getInstance()->createNode("MenuBackground.csb");

	float scaleX = m_screenSize.width / background->getContentSize().width;
	float scaleY = m_screenSize.height / background->getContentSize().height;

	background->setScale(scaleX, scaleY);
	background->setAnchorPoint(Vec2(0.5f, 0.5f));
	background->setPosition(Vec2(m_screenSize.width / 2, m_screenSize.height / 2));

	this->addChild(background);
}

void MenuManager::initComponent()
{
	m_btnSetting = dynamic_cast<Button*>(m_node->getChildByTag(EMenuCocoTag::kMenuCoco_Button)->getChildByTag(EMenuCocoTag::kMenuCoco_btnSetting));
	m_pnlSetting = dynamic_cast<Node*>(m_node->getChildByTag(EMenuCocoTag::kMenuCoco_Button)->getChildByTag(EMenuCocoTag::kMenuCoco_pnlSetting));
}

void MenuManager::initButton()
{
	//Close
	Button* btnClose = dynamic_cast<Button*>(m_pnlSetting->getChildByTag(EMenuCocoTag::kMenuCoco_btnClose));
	btnClose->addClickEventListener(CC_CALLBACK_1(MenuManager::tapClose, this));

	//Setting
	Button* btnSetting = dynamic_cast<Button*>(m_btnSetting);
	btnSetting->addClickEventListener(CC_CALLBACK_1(MenuManager::tapSetting, this));

	//Hard - Normal - Easy
	Button* btnHard = dynamic_cast<Button*>(m_node->getChildByTag(EMenuCocoTag::kMenuCoco_Button)->getChildByTag(EMenuCocoTag::kMenuCoco_btnHard));
	Button* btnNormal = dynamic_cast<Button*>(m_node->getChildByTag(EMenuCocoTag::kMenuCoco_Button)->getChildByTag(EMenuCocoTag::kMenuCoco_btnNormal));
	Button* btnEasy = dynamic_cast<Button*>(m_node->getChildByTag(EMenuCocoTag::kMenuCoco_Button)->getChildByTag(EMenuCocoTag::kMenuCoco_btnEasy));

	btnHard->addClickEventListener(CC_CALLBACK_1(MenuManager::tapMode, this));
	btnNormal->addClickEventListener(CC_CALLBACK_1(MenuManager::tapMode, this));
	btnEasy->addClickEventListener(CC_CALLBACK_1(MenuManager::tapMode, this));
}

void MenuManager::tapMode(cocos2d::Ref *pSender)
{
    if(!m_isProcressing)
    {
        Button* btn = dynamic_cast<Button*>(pSender);
        switch (btn->getTag())
        {
            case EMenuCocoTag::kMenuCoco_btnHard:
                LevelManager::s_mode = EGameMode::kMode_Hard;
                break;
            case EMenuCocoTag::kMenuCoco_btnNormal:
                LevelManager::s_mode = EGameMode::kMode_Normal;
                break;
            case EMenuCocoTag::kMenuCoco_btnEasy:
                LevelManager::s_mode = EGameMode::kMode_Easy;
                break;
        }
        m_isProcressing = true;
        SceneManager::getInstance()->changeScene(this, EScene::kScene_Level);
    }
}

void MenuManager::tapClose(cocos2d::Ref *pSender)
{
	m_pnlSetting->setVisible(false);
}

void MenuManager::tapSetting(cocos2d::Ref *pSender)
{
	Point p = m_pnlSetting->getPosition();
	m_pnlSetting->setOpacity(0);
	m_pnlSetting->setVisible(true);
	//pnlSettings->setScale(0.2f);
	m_pnlSetting->runAction(FadeIn::create(0.2f));
	m_pnlSetting->setPosition(m_pnlSetting->getPosition().x - 100, m_pnlSetting->getPosition().y);
	m_pnlSetting->runAction(MoveTo::create(0.2f, p));
	//pnlSettings->runAction(ScaleTo::create(0.2f,1.0f));
}
